package com.yksj.healthtalk.ui.interestwall;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.yksj.healthtalk.entity.TagEntity;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.ui.interestwall.StairCategoryChooseFragment.CategoryChooseAdapter;
import com.yksj.healthtalk.utils.XmlCategoryParserUtils;

/**
 * 按分类查找的fragment，关联类{@link StairCategoryChooseFragment (该类为一级分类选择)}}
 * 二级分类
 * @author crj
 * 
 */
public class CategoryChooseFragment extends Fragment implements
		OnItemClickListener,OnClickListener {
	/**
	 * 2级分类展示listview
	 */
	private ListView secondaryListView;
	/**
	 * listview展示的数据源
	 */
	private List<TagEntity> scondaryData;
	/**
	 * 展示适配器
	 */
	private CategoryChooseAdapter adapter;
	private TagEntity entity;
	/**
	 * xml文件id
	 */
	private String ids;
	
	/**
	 * 监听者模式，监听fragment点击返回给activity
	 */
//	private FragmentItemClickListener mClickListener;

	public static CategoryChooseFragment newInstance(String id) {
		CategoryChooseFragment f = new CategoryChooseFragment();
		Bundle mBundle = new Bundle();
		mBundle.putString("id", id);
		f.setArguments(mBundle);
		return f;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ids = this.getArguments().getString("id");
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.category_choose_main,
				container, false);
		initData();
		initView(view);
		return view;
	}

	/**
	 * 初始化title
	 * @param v
	 */
	public void initTitle(View v) {
		Button backButton = (Button) v.findViewById(R.id.title_back);
		backButton.setOnClickListener(this);
		TextView textView = (TextView) v.findViewById(R.id.title_lable);
		textView.setText("分类选择");
	}
	
	public void initData() {
		/**
		 * 通过xml文件获取二级分类信息
		 */
		scondaryData = XmlCategoryParserUtils.getCategoryData(getResources(), R.xml.interestwall_class_store,Integer.parseInt(ids));
		adapter = new CategoryChooseAdapter(getActivity(), scondaryData);
	}

	public void initView(View view) {
		initTitle(view);
		secondaryListView = (ListView) view
				.findViewById(R.id.category_listview);
		secondaryListView.setAdapter(adapter);
		secondaryListView.setOnItemClickListener(this);
		secondaryListView.setSelector(R.color.transparent);
//		view.findViewById(R.id.category_photogallery).setVisibility(View.GONE);
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position,
			long id) {
			entity = scondaryData.get(position);
			/*//当oldBeClickItem！=-1时表示存储了上一次点击的position
			hideFragment();
			if(mClickListener!=null) {
				mClickListener.fragmentClick(entity);
			}*/
			Intent intent=new Intent(this.getActivity(),InterestMainFragment.class);
			intent.putExtra("entity", entity);
			getActivity().setResult(Activity.RESULT_OK, intent);
			getActivity().finish(); 
	}
	
	/**
	 * 删除当前fragment
	 */
	public void hideFragment() {
		FragmentManager manager = getFragmentManager();
		FragmentTransaction fragmentTransaction = manager.beginTransaction();
		if(this.isAdded()) {
			fragmentTransaction.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right);
			fragmentTransaction.remove(this);
		}
		fragmentTransaction.commit();
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()) {
		case R.id.title_back:
			getFragmentManager().popBackStackImmediate();
			break;
		}
	}

/*	public void setFragmentItemClickListener(FragmentItemClickListener clickListener) {
		this.mClickListener = clickListener;
	}*/
	
	/**
	 * 观察者模式监听器,监听某个类被点击，传入参数为点击的分类的实体
	 * @author crj
	 *
	 */
	public interface FragmentItemClickListener {
		public void fragmentClick(TagEntity entity);
	}
	
}
